import { GrammyError, HttpError } from "grammy";

export class ErrorHandler {
  /** @param {import("grammy/out/bot").Bot} bot */
  handle(bot) {
    bot.catch(this.#catch);
  }

  /** @param {import("grammy/out/bot").BotError} err */
  #catch(err) {
    const ctx = err.ctx;

    console.error(`Error while handling update ${ctx.update.update_id}:`);
    const e = err.error;

    if (e instanceof GrammyError) {
      console.error("Error in request:", e.description);
    } else if (e instanceof HttpError) {
      console.error("Could not contact Telegram:", e);
    } else {
      console.error("Unknown error:", e);
    }
  }
}
